export default {
  data() {
    return {
      coinSearch: "",
      baseCoin: require("../../assets/img/coin/baseCoin.png"),
      pages: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      Tokens: [],
      TokensList: []
    };
  },
  computed: {
    StoreList() {
      return this.$store.state.tokenList;
    },
    LoadingSucc() {
      return this.$store.state.tokenListSuccess;
    }
  },
  watch: {
    StoreList: {
      handler(val) {
        this.TokensList = val.concat();
        if (val.length > 0) {
          this.init();
        }
      },
      immediate: true
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (this.TokensList.length <= 0) return;
      this.pages.total = this.TokensList.length;
      let start = (this.pages.currentPage - 1) * this.pages.pageSize;
      let end =
        this.pages.currentPage * this.pages.pageSize > this.pages.total ?
        this.pages.total :
        this.pages.currentPage * this.pages.pageSize;
      this.Tokens = this.TokensList.slice(start, end);
    },
    searchToken(token) {
      let tokenExsit = false;
      this.TokensList.forEach(item => {
        if (!tokenExsit) {
          if (item.symbol === token) {
            tokenExsit = true;
          } else {
            tokenExsit = false;
          }
        }
      });
      if (tokenExsit) {
        this.$router.push({
          path: "/tokenDetail",
          query: {
            TokenName: token
          }
        });
      } else {
        this.$notify({
          title: this.$t('components.tip'),
          message: this.$t('components.errToken'),
          type: "warning"
        });
      }
    },
    sortToken(sort) {
      if (sort.order === "ascending") {
        this.TokensList = this.TokensList.sort(this.compare("holders", true));
      } else if (sort.order === "descending") {
        this.TokensList = this.TokensList.sort(this.compare("holders", false));
      } else {
        this.TokensList = this.StoreList.concat();
      }
      this.init();
    },
    compare(attr, rev) {
      if (rev == undefined) {
        rev = 1;
      } else {
        rev = rev ? 1 : -1;
      }
      return function (a, b) {
        a = a[attr];
        b = b[attr];
        if (a < b) {
          return rev * -1;
        }
        if (a > b) {
          return rev * 1;
        }
        return 0;
      };
    },
    //token搜索列表
    querySearch(queryString, cb) {
      let coinName = this.TokensList;
      let results = coinName.filter(data =>
        data.value.toLowerCase().includes(queryString.toLowerCase())
      );
      cb(results);
    },
    introFilter(item, lang = this.$store.state.initLang) {
      let language = lang
      switch (lang) {
        case 'zh':
          language = 'zh-CN'
          break
        case 'ja':
          break
        case 'en':
          language = 'en-US'
          break
        default:
          break;
      }
      return item.introduce && JSON.parse(item.introduce)[language] !== "" ?
        JSON.parse(item.introduce)[language] :
        item.introduction;
    },
    pageChange(page) {
      this.pages.currentPage = page;
      this.init();
    },
    sizeChange(size) {
      this.pages.currentPage = 1;
      this.pages.pageSize = size;
      this.init();
    }
  }
};